var express = require('express');
var md5 = require('./../tools/md5.js')
var AES =require('./../tools/AES.js')
var getTime=require('./../tools/getTime.js')
var router = express.Router();

var db = require('../sqldb/index.js');
var patient =require('../model/patient.js')
var department =require('./../model/department.js')


function jsonSort(json,key){
	for(var j=1,jl=json.length;j < jl;j++){
		var temp = json[j],
		val  = temp[key],
		i    = j-1;
		while(i >=0 && json[i][key]>val){
			json[i+1] = json[i];
			i = i-1;    
		}
		json[i+1] = temp;

	}
	return json;
}

/* GET users listing. */
router.post('/', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		var pqueue =''
		if (req.body.ifMedical) {
			pqueue='2诊列队'
		}else{
			pqueue='首诊列队'
		}
		return patient(db.sequelize).create({
			pName:req.body.pName,
			pIdNum:req.body.pIdNum,
			pSex:req.body.pSex,
			pAge:req.body.pAge,
			pDept:req.body.pDept,
			diseaseDescription:req.body.diseaseDescription,
			ifMedical:req.body.ifMedical,
			pQueue:pqueue,
			registerTime:getTime()
		},{
			transaction:t
		}).then(function(data){
			res.json({
				status:1,
				msg:'',
				result:data
			})
		}).catch(function(err){
			res.json({
				status:0,
				msg:err,
				result:''
			})
			console.log("发生错误：" + err);
		});
	})
});

router.post('/department', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return department(db.sequelize).findAll({
			attributes: ['dName']
		},{
			transaction:t
		}).then(function(data){
			res.json({
				status:1,
				msg:'',
				result:data
			})
		}).catch(function(err){
			res.json({
				status:0,
				msg:err,
				result:''
			})
			console.log("发生错误：" + err);
		});
	})
});

router.post('/get', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return patient(db.sequelize).findOne({
			where:{
				pCardNum:req.body.pCardNum
			}
		},{
			transaction:t
		}).then(function(data){
			res.json({
				status:1,
				msg:'',
				result:data
			})
		}).catch(function(err){
			res.json({
				status:0,
				msg:err,
				result:''
			})
			console.log("发生错误：" + err);
		});
	})
});

router.post('/frontPeople', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return patient(db.sequelize).findAll({
			where:{
				pQueue:'候诊列队',
				pDept:req.body.pDept,
				pConsultingRoom:req.body.pConsultingRoom
			}
		},{
			transaction:t
		}).then(function(data){
			var frontPeople=0
			for (var i = 0; i < data.length; i++) {
				console.log(i)
				if (req.body.pCardNum==data[i].pCardNum) {
					console.log(i)
					frontPeople=i
				}
			}
			res.json({
				status:1,
				msg:'',
				result:{frontPeople:frontPeople}
			})
		}).catch(function(err){
			res.json({
				status:0,
				msg:err,
				result:''
			})
			console.log("发生错误：" + err);
		});
	})
});



// router.get('/expless', function(req, res, next) {
// 	return db.sequelize.transaction(function(t){
// 		console.log("+++++++++++++++++++")
// 		return patient(db.sequelize).create({
// 			pName:'假的',
// 			pIdNum:12315614516,
// 			pSex:'男',
// 			pAge:'12',
// 			pDept:'神经科',
// 			pConsultingRoom:'一号诊室',
// 			diseaseDescription:'假的',
// 			ifMedical:1,
// 			pQueue:'候诊列队',
// 			registerTime:getTime(),
// 			triageTime:getTime(),
// 		},{
// 			transaction:t
// 		}).then(function(data){
// 			res.json({
// 				status:1,
// 				msg:'',
// 				result:data
// 			})
// 		}).catch(function(err){
// 			res.json({
// 				status:0,
// 				msg:err,
// 				result:''
// 			})
// 			console.log("发生错误：" + err);
// 		});
// 	})
// });


module.exports = router;
